using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using SCSEngine.ScreenManagement.Implement;
using SCSEngine.ScreenManagement;
using SCSEngine.Services;
using SCSEngine.Services.Input;
using SCSEngine.ResourceManagement;
using SCSEngine.Control;
using SCSEngine.Sprite;
using SCSEngine.Sprite.Implements;
using TriUan.TriUanGame.Effect;
using Microsoft.Xna.Framework.Input.Touch;
using TextEffect;
using SVLvsXNA;
using SVLvsXNA.TriUanGame;
using Microsoft.Xna.Framework.Input;

namespace TriUan.TriUanGame.GameScreen
{
    public class GameModeScreen : BaseGameScreen
    {
        protected enum Mode
        {
            CLASSIC,
            FREESTYLE,
            CREATIVE,
            NONE
        }

        private SCSServices services;

        private IResourceManager resourceManager;

        private Texture2D background;
        public Texture2D Background
        {
            set { this.background = value; }
        }

        public ListView.ListView ListView
        {
            get;
            set;
        }

        private Button classic, creative, freeStyle;
        public Button Classic
        {
            get { return this.classic; }
        }

        public Button Creative
        {
            get { return this.creative; }
        }

        public Button FreeStyle
        {
            get { return this.freeStyle; }
        }

        private Button buttonLeft, buttonRight;
        public Button ButtonLeft
        {
            get { return this.buttonLeft; }
        }

        public Button ButtonRight
        {
            get { return this.buttonRight; }
        }

        private Button buttonBack;

        public ISprite icon { get; set; }
        public Vector2 iconPosition { get; set; }
        public Vector2 iconOrgin { get; set; }
        public float iconScale { get; set; }

        public CTextEffect textEffect;
        public SpriteFont font;
        public Rectangle textBox;
        public String textInformation;
        protected bool isChoosedMode;
        protected Mode mode;

        protected List<TriUan.TriUanGame.Effect.Effect> starEffect;
        protected List<Sprite> sprites;

        public GameModeScreen(IGameScreenManager manager)
            : base(manager)
        {
            services = (SCSServices)manager.GamePage.Services.GetService(typeof(SCSServices));

            resourceManager = (IResourceManager)manager.GamePage.Services.GetService(typeof(IResourceManager));

            this.Renew();
        }

        public override void Update(GameTime gameTime)
        {
            if (this.GamePage.IsBackKeyPress)
            {
                if (isChoosedMode)
                {
                    this.Renew();
                }
                else
                {
                    Manager.RemoveCurrent();
                }

                this.GamePage.IsBackKeyPress = false;
            }

            ListView.HandleInput(services.InputHandle.Gestures);
            ListView.Update(gameTime);
            if (textEffect.BoxText != null)
                textEffect.UpdateDrawAlphaBox(gameTime);
            foreach (GestureSample sample in services.InputHandle.Gestures)
            {
                if (sample.GestureType == GestureType.Tap)
                {
                    starEffect.Add(new StarEffect(Manager.GamePage, services.InputHandle.Gestures[0].Position, 10, sprites, true, 50, Color.White));
                }
            }

            for (int i = 0; i < starEffect.Count; i++)
            {
                starEffect[i].Update(gameTime);
            }

            for (int i = 0; i < starEffect.Count; i++)
            {
                if (starEffect[i].IsCompleted)
                {
                    starEffect.RemoveAt(i);
                }
            }

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            this.services.SpriteBatch.Draw(background, Vector2.Zero, Color.White);
            if (icon != null)
                this.services.SpritePlayer.Draw(icon, iconPosition, 0f, iconScale, iconOrgin, Color.White, SpriteEffects.None, 1.0f);

            ListView.Draw(gameTime);

            for (int i = 0; i < starEffect.Count; i++)
            {
                starEffect[i].Draw(gameTime);
            }

            //Draw text
            if (textEffect.BoxText!=null)
                    textEffect.DrawAlphaBox(Color.Green, Color.Black);

            base.Draw(gameTime);
        }

        private void Renew()
        {
            Components.Clear();
            
            //Init textEffect

            // Load sprite for effect
            sprites = new List<Sprite>();
            sprites.Add((Sprite)resourceManager.GetResource<ISprite>("Square"));
            starEffect = new List<Effect.Effect>();

            // Initialize ListView
            ListView = new ListView.ListView(new Vector2(800, 480),
                new Vector2(10f, 346f), new Vector2(0f, 0f), new Vector2(0,0), 0.002f, false);

            // Icon in choose Mode of mode
            icon = null;
            iconPosition = new Vector2(60f, 295f);
            iconScale = 1f;

            // Default mode is none
            this.mode = Mode.NONE;
            this.isChoosedMode = false;

            // Get background
            this.background = resourceManager.GetResource<Texture2D>(Language.Prep + "GameMode");

            // Create button choose classic mode
            Sprite classicSprite = (Sprite)resourceManager.GetResource<ISprite>("ScareCrow_Classic");
            classicSprite.Play();
            classicSprite.TimeDelay = new TimeSpan(1000000);
            classic = new Button(this.GamePage, classicSprite);
            classic.Position = new Vector2(106f, 169f);
            classic.Size = new Vector2(232f, 224f);
            classic.OnTouched += new OnButtonTouchedHandler(classic_OnTouched);

            // Create button choose creative mode
            Sprite createSprite = (Sprite)resourceManager.GetResource<ISprite>("ScareCrow_Creative");
            createSprite.Play();
            createSprite.TimeDelay = new TimeSpan(1000000);
            creative = new Button(this.GamePage, createSprite);
            creative.Position = new Vector2(346f, 211f);
            creative.Size = new Vector2(187f, 177f);
            creative.OnTouched += new OnButtonTouchedHandler(creative_OnTouched);

            // Create button choose freestyle mode
            Sprite freeStyleSprite = (Sprite)resourceManager.GetResource<ISprite>("ScareCrow_FreeStyle");
            freeStyleSprite.Play();
            freeStyleSprite.TimeDelay = new TimeSpan(1000000);
            freeStyle = new Button(this.GamePage, freeStyleSprite);
            freeStyle.Position = new Vector2(563f, 168f);
            freeStyle.Size = new Vector2(249f, 238f);
            freeStyle.OnTouched += new OnButtonTouchedHandler(freeStyle_OnTouched);

            // Create left button in mode of mode
            buttonLeft = new Button(this.GamePage, null);
            buttonLeft.Position = new Vector2(230f, 265f);
            buttonLeft.OnTouched += new OnButtonTouchedHandler(buttonLeft_OnTouched);
            buttonLeft.Visible = false;
            buttonLeft.Enabled = false;

            // Create right button in mode of mode
            buttonRight = new Button(this.GamePage, null);
            buttonRight.Position = new Vector2(455f, 265f);
            buttonRight.OnTouched += new OnButtonTouchedHandler(buttonRight_OnTouched);
            buttonRight.Visible = false;
            buttonRight.Enabled = false;

            // Back button to menu
            buttonBack = new Button(this.GamePage, resourceManager.GetResource<ISprite>("Arrow"));
            buttonBack.Scale = 0.5f;
            buttonBack.Size = new Vector2(58f, 33f);
            buttonBack.Position = new Vector2(0f, 0f);
            buttonBack.OnTouched += new OnButtonTouchedHandler(buttonBack_OnTouched);

            //Init textEffect
            font = resourceManager.GetResource<SpriteFont>("InformationFont");
            textBox = new Rectangle(195, 95, 450, 120);
            textEffect = new CTextEffect(services.SpriteBatch, font, new Vector2(textBox.X + 2, textBox.Y + textBox.Height), textBox);

            Components.Add(classic);
            Components.Add(creative);
            Components.Add(freeStyle);
            Components.Add(buttonLeft);
            Components.Add(buttonRight);
            Components.Add(buttonBack);
        }

        void buttonBack_OnTouched(Button sender, EventArgs args)
        {
            if (isChoosedMode)
            {
                this.Renew();
            }
            else
            {
                Manager.RemoveCurrent();
            }
        }

        void buttonRight_OnTouched(Button sender, EventArgs args)
        {
            switch (this.mode)
            {
                case Mode.CLASSIC:
                    break;
                case Mode.CREATIVE:
                    break;
                case Mode.FREESTYLE:
                    Manager.AddExclusive(Manager.Bank.GetScreen("Shake"));
                    break;
            }
        }

        void buttonLeft_OnTouched(Button sender, EventArgs args)
        {
            switch (this.mode)
            {
                case Mode.CLASSIC:
                    Manager.AddExclusive(Manager.Bank.GetScreen("GamePlay"));
                    break;
                case Mode.CREATIVE:
                    break;
                case Mode.FREESTYLE:
                    break;
            }
        }

        void freeStyle_OnTouched(Button sender, EventArgs args)
        {
            //this.textInformation = GameInformation.GetFreeStyleMode();
            //this.textEffect.InitDrawAlphaBox(true, textInformation, 20.0f);
            this.mode = Mode.FREESTYLE;
            Modes.FreeStyleMode.getInst(this.GamePage).Change(this, this.Manager);
            this.isChoosedMode = true;

            ListView.Enabled = true;
            ListView.Visibled = true;
        }

        void creative_OnTouched(Button sender, EventArgs args)
        {
            this.textInformation = GameInformation.GetCreateMode();
            this.textEffect.InitDrawAlphaBox(true, textInformation, 20.0f);
            this.mode = Mode.CREATIVE;
            Modes.CreativeMode.getInst(this.GamePage).Change(this, this.Manager);
            this.isChoosedMode = true;

            ListView.Enabled = true;
            ListView.Visibled = true;
        }

        void classic_OnTouched(Button sender, EventArgs args)
        {
            this.textInformation = GameInformation.GetClassicMode();
            this.textEffect.InitDrawAlphaBox(true, textInformation, 20.0f);
            this.mode = Mode.CLASSIC;
            Modes.ClassicMode.getInst(this.GamePage).Change(this, this.Manager);
            this.isChoosedMode = true;

            ListView.Enabled = true;
            ListView.Visibled = true;
            
        }
    }
}
